/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.wikimark.container;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

final class HttpServletResponseImpl implements HttpServletResponse {
	
	private ServletBufferedPipe out;
	private Reader reader;
	private PrintWriter writer;
	private String encoding = "utf-8";
	private boolean commited;
	
	final Map<String, Cookie> cookies = new HashMap<String, Cookie>();
	private String contentType;
	
	Reader getReader() throws IOException {
		if (reader == null && out != null) {
			try {
				reader = new InputStreamReader(out.readStream, encoding);
			} catch (UnsupportedEncodingException e) {
				throw new RuntimeException(e);
			}
		}
		return reader;
	}

	public void addCookie(Cookie cookie) {		
		cookies.put(cookie.getName(), cookie);
	}

	public void addDateHeader(String name, long date) {
		if (true) throw new UnsupportedOperationException("not implemented");
		
	}

	public void addHeader(String name, String value) {
		if (true) throw new UnsupportedOperationException("not implemented");
		
	}

	public void addIntHeader(String name, int value) {
		if (true) throw new UnsupportedOperationException("not implemented");
		
	}

	public boolean containsHeader(String name) {
		if (true) throw new UnsupportedOperationException("not implemented");
		return false;
	}

	public String encodeRedirectURL(String name) {
		if (true) throw new UnsupportedOperationException("not implemented");
		return null;
	}

	public String encodeRedirectUrl(String url) {
		if (true) throw new UnsupportedOperationException("not implemented");
		return null;
	}

	public String encodeURL(String url) {
		if (true) throw new UnsupportedOperationException("not implemented");
		return null;
	}

	public String encodeUrl(String url) {
		if (true) throw new UnsupportedOperationException("not implemented");
		return null;
	}

	public void sendError(int sc) throws IOException {
		if (true) throw new UnsupportedOperationException("not implemented");
		
	}

	public void sendError(int sc, String msg) throws IOException {
		if (true) throw new UnsupportedOperationException("not implemented");
		
	}

	public void sendRedirect(String location) throws IOException {		
	}

	public void setDateHeader(String name, long date) {
		if (true) throw new UnsupportedOperationException("not implemented");
		
	}

	public void setHeader(String name, String value) {		
	}

	public void setIntHeader(String name, int value) {
		if (true) throw new UnsupportedOperationException("not implemented");
		
	}

	public void setStatus(int sc) {		
		if (sc % 100 > 3) {
			throw new RuntimeException("Servlet error: " + sc);
		}		
	}

	public void setStatus(int sc, String msg) {
		setStatus(sc);
	}

	public void flushBuffer() throws IOException {
		commited = true;		
	}

	public int getBufferSize() {
		if (true) throw new UnsupportedOperationException("not implemented");
		return 0;
	}

	public String getCharacterEncoding() {
		return encoding;
	}

	public String getContentType() {
		return contentType;
	}

	public Locale getLocale() {
		if (true) throw new UnsupportedOperationException("not implemented");
		return null;
	}

	public ServletOutputStream getOutputStream() throws IOException {
		if (out == null) {
			out = new ServletBufferedPipe();
		}
		return out;
	}

	public PrintWriter getWriter() throws IOException {
		if (writer == null) {
			writer = new PrintWriter(new OutputStreamWriter(getOutputStream()));
		}
		return writer;
	}

	public boolean isCommitted() {
		return commited;
	}

	public void reset() {
		if (true) throw new UnsupportedOperationException("not implemented");
		
	}

	public void resetBuffer() {
		if (true) throw new UnsupportedOperationException("not implemented");
		
	}

	public void setBufferSize(int size) {
		if (true) throw new UnsupportedOperationException("not implemented");
		
	}

	public void setCharacterEncoding(String charset) {
		encoding = charset;
	}

	public void setContentLength(int len) {
		if (true) throw new UnsupportedOperationException("not implemented");
		
	}

	public void setContentType(String type) {
		contentType = type;
	}

	public void setLocale(Locale locale) {
		if (true) throw new UnsupportedOperationException("not implemented");
		
	}
	
}
